<template>
  <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="600px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="auto">
      <el-row>
        <el-col :span="18">
          <el-form-item label="业务线" prop="businessLineId">
            <el-select v-model="form.businessLineId" placeholder="请选择业务线" clearable>
              <el-option
                v-for="dict in dict.type.sys_business_line"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="项目类型" prop="projectType">
            <el-select v-model="form.projectType" placeholder="请选择项目类型" clearable>
              <el-option
                v-for="dict in dict.type.project_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="合同金额" prop="contractAmount">
            <el-input-number
              controls-position="right"
              :step="100"
              :min="0"
              style="width: 150px"
              :precision="2"
              v-model="form.contractAmount"
              placeholder="合同金额"
            >
            </el-input-number>
            元
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="open = false">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
export default {
  name: 'Split',
  props: {
    show: {
      type: Boolean,
      default: false,
    },
    id: {
      type: String || Number,
      default: null,
    },
    dataSource: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {
      open: false,
      title: '',
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        businessLineId: [{ required: true, message: '业务线不能为空', trigger: 'change' }],
        projectType: [{ required: true, message: '项目类型不能为空', trigger: 'blur' }],
        contractAmount: [{ required: true, message: '请输入合同金额', trigger: 'blur' }],
      },
    }
  },
  dicts: ['sys_business_line','project_type'],
  watch: {
    show: {
      handler(val) {
        this.open = val
      },
    },
    open: {
      handler(val) {
        this.reset()
        if (val) {
          this.title = this.dataSource.contractSplitId ? '修改合同拆分' : '添加合同拆分'
          if (this.dataSource) {
            this.form = JSON.parse(JSON.stringify(this.dataSource))
            console.log(this.form)
          }
        } else {
          this.$emit('cancel', false)
        }
      },
    },
  },
  methods: {
    // 表单重置
    reset() {
      this.form = {
        businessLineId:null,
        projectType:null,
        contractSplitId: null,
        expenseType: null,
        contractAmount: null,
        num: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    handleAddMoreInfo() {},
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.$emit('save', { name: 'contractSplitList', data: JSON.parse(JSON.stringify(this.form)) })
        }
      })
    },
  },
}
</script>
